/*!
 * Playground
 *
 * Author: Shigerello
 * Make sure that this script is executed AFTER "window" was loaded.
 */

// Insert this <script> tag into HTML header.
/*
 <script async="true" type='text/javascript'>
 // ##### Event #####
 var PLAYGROUND = (function(me) {
 if (window.addEventListener) {
 me.addEvent = function(element, event, fn) {
 element.addEventListener(event, fn, false);
 };
 } else {
 me.addEvent = function(element, event, fn) {
 element.attachEvent('on' + event, fn);
 };
 }

 // Main function.
 // This function is invoked when document.body is ready, so override this between <body> and </body>).
 me.main = function() {
 };

 me.play = function() {
 // Ensure that init() be executed when document.doby is ready; PLAYGROUND.main() should be already overridden at that time.
 if (document.body) {
 init();
 } else {
 PLAYGROUND.addEvent(window, "load", init);
 }

 var elem = document.createElement("script");
 function init() {
 elem.async = "true";
 elem.src = "//contender-x-blogger.googlecode.com/git/js/playground-init.js";
 PLAYGROUND.addEvent(elem, "load", function() {
 document.body.removeChild(elem);
 });
 document.body.appendChild(elem);
 }
 };

 return me;
 } )(PLAYGROUND || {});

 PLAYGROUND.play();
 </script>
 */

PLAYGROUND = (function(me) {
	// ##### jQuery #####
	var elem = document.createElement("script");
	elem.async = "true";
	elem.src = "//code.jquery.com/jquery-1.10.2.min.js";
	// ##### IMPORTANT #####
	// "load" event of jQuery <script> tag triggers PLAYGROUND.main() to be
	// executed.
	PLAYGROUND.addEvent(elem, "load", function() {
		document.body.removeChild(elem);
		initJQuery();
		// Load jQuery UI
		$.getScriptComp("//code.jquery.com/ui/1.10.3/jquery-ui.min.js");
		var css = document.createElement("link");
		css.type = "stylesheet";
		css.href = "//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css";
		document.head.appendChild(css);
		// Load SyntaxHighlighter
		initSyntaxHighlighter();
		PLAYGROUND.main();
	});
	document.body.appendChild(elem);
	function initJQuery() {
		$.ajaxSetup({
			cache : true
		});
		// accept "text/css" MIME type
		// TODO: create a hack for "css" transports, like for "script"
		// transports
		$.ajaxSettings.accepts['css'] = 'text/css';
		$(document).ajaxError(function(e, xhr, settings, exception) {
			console.log('jQuery error: ' + settings.url + ':\n' + exception);
		});
		// Shorthand
		$.getScriptComp = function(url, complete) {
			$.ajax({
				url : url,
				dataType : "script",
				complete : complete
			});
		};
	}

	// ##### SyntaxHighlighter #####
	function initSyntaxHighlighter() {
		function path() {
			var args = arguments, result = [];
			for (var i = 0; i < args.length; ++i)
				result.push(args[i].replace('@',
						'http://alexgorbatchev.com/pub/sh/current/scripts/'));

			return result;
		}
		;

		// NOTE: If I could load stylesheets asynchronously in jQuery...
		var hsCssPath = path('@../styles/shCore.css',
				'@../styles/shThemeEmacs.css');
		var elem = document.createElement("link");
		elem.rel = "stylesheet";
		elem.href = hsCssPath[0];
		document.head.appendChild(elem);
		elem = document.createElement("link");
		elem.rel = "stylesheet";
		elem.href = hsCssPath[1];
		document.head.appendChild(elem);

		// SH brushes requires global SyntaxHighlighter object; instantiate it
		// first by loading shCore.js.
		$
				.getScript(path('@shCore.js'))
				.fail(
						function() {
							console
									.debug('error: failed to load SyntaxHilighter core');
						})
				.done(
						function() {
							// Now global object SyntaxHighlighter is available,
							// ready to load brushes.
							// Good for debugging: SyntaxHighlighter
							var paths = path('@shBrushCpp.js',
									'@shBrushBash.js', '@shBrushXml.js',
									'@shBrushJScript.js', '@shBrushCSharp.js',
									'@shBrushPython.js', '@shBrushCss.js');
							$.when
									.apply(null, paths.map($.getScript))
									.fail(
											function() {
												console
														.debug('error: failed to load SyntaxHilighter bruses');
											}).done(function() {
										// All the specified brushes were
										// loaded, now it's time for syntax
										// highlighting
										// *ONLY* if document.body was already
										// loaded.
										// Good for debugging:
										// SyntaxHighlighter.brushes
										shLoaded();
									});
						});

		// SyntaxHighlighter line wrap hack
		// (original:
		// https://bitbucket.org/alexg/syntaxhighlighter/issue/182/version-3-making-code-wrap)
		function wrap() {
			var shList = $('.syntaxhighlighter');
			for (var j = 0; j < shList.length; ++j) {
				var sh = $(shList[j]);
				var gLines = sh.find('.gutter .line');
				var cLines = sh.find('.code .line');
				for (var i = 0; i < gLines.length; ++i) {
					var h = $(cLines[i]).height();
					if (h > $(gLines[i]).height()) {
						gLines[i].setAttribute('style', 'height: ' + h
								+ 'px !important;');
					}
				}
			}
		}
		;

		function shLoaded() {
			// SyntaxHighlighter config
			SyntaxHighlighter.config.bloggerMode = true;

			// Triggered after document.body was loaded because jQuery needs to
			// interact with loaded <body> contents
			if (document.body == undefined) {
				me.addEvent(window, 'load', function() {
					SyntaxHighlighter.highlight();
					wrap();
				});
			} else {
				SyntaxHighlighter.highlight();
				wrap();
			}

			// Triggered when window was resized.
			me.addEvent(window, 'resize', function() {
				wrap();
			});
		}
	}

	// Extension of Playground was done, return it to finish.
	return me;
})(PLAYGROUND || {});
